package com.dyb.recuitment.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dyb.recuitment.common.Result;
import com.dyb.recuitment.entity.Job;
import com.dyb.recuitment.entity.Setting;
import com.dyb.recuitment.entity.User;
import com.dyb.recuitment.entity.UserLog;
import com.dyb.recuitment.service.ISettingService;
import com.dyb.recuitment.service.IUserLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 隐私设置表 前端控制器
 * </p>
 *
 * @author 邓宇斌
 * @since 2022-02-18
 */
@RestController
@Api(tags = "隐私设置管理(Setting)")
@RequestMapping("/setting")
public class SettingController {

    @Autowired
    private ISettingService settingService;

    @Autowired
    private IUserLogService userLogService;

    @ApiOperation(value = "添加隐私设置", notes = "通过用户ID、隐藏信息字典值、允许推荐字典值添加隐私设置" +
            "{\"userId\":1,\"is_hidden\":0,\"is_recommend\":1}")
    @ApiImplicitParam(name = "setting", value = "Setting类")
    @PostMapping("/add")
    public Object add(@RequestBody Setting setting) {
        Object data = null;
        try {
            data = settingService.add(setting);
            userLogService.add(new UserLog(setting.getUserId(), "添加隐私设置"));
            if (data == null || (int) data == 0) {
                return Result.failure(data, "添加隐私设置失败!");
            }
            return Result.success(data, "添加隐私设置成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "修改隐私设置", notes = "通过用户ID、隐藏信息字典值、允许推荐字典值修改隐私设置" +
            "{\"userId\":1,\"is_hidden\":0,\"is_recommend\":1}")
    @ApiImplicitParam(name = "setting", value = "Setting类")
    @PostMapping("/update")
    public Object update(@RequestBody Setting setting) {
        Object data = null;
        try {
            data = settingService.update(setting);
            userLogService.add(new UserLog(setting.getUserId(), "修改隐私设置"));
            if (data == null || data == (Integer) 0) {
                return Result.failure(data, "设置失败!");
            }
            return Result.success(data, "设置成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "查询隐私设置", notes = "按用户ID查询隐私设置 {\"userId\":1}")
    @ApiImplicitParam(name = "setting", value = "Setting类")
    @PostMapping("/getByUserId")
    public Object getByUserId(@RequestBody Setting setting) {
        Object data = null;
        try {
            data = settingService.selectBySetting(setting);
            userLogService.add(new UserLog(setting.getUserId(), "查询隐私设置"));
            if (data == null || ((List) data).size() <= 0) {
                return Result.failure(data, "查询失败");
            }
            return Result.success(data, "查询成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

}
